/**
 * 学习打卡相关API接口
 */

import type {
  ApiResponse,
  PageResult,
  PunchAchievementRespVO,
  PunchCalendarRespVO,
  PunchCreateReqVO,
  PunchExportParams,
  PunchRankingRespVO,
  PunchRecordRespVO,
  PunchReminderSettings,
  PunchRespVO,
  PunchShareInfo,
  PunchStatsRespVO,
  PunchTemplate,
  PunchUpdateReqVO,
} from './types/punch'
import { http } from '@/utils/request'

/**
 * 创建学习打卡
 */
export function createPunch(data: PunchCreateReqVO) {
  return http.post<ApiResponse<number>>('/reading/punch/create', data)
}

/**
 * 获取打卡记录列表
 */
export function getPunchList(params?: { limit?: number }) {
  return http.get<ApiResponse<PunchRecordRespVO[]>>('/reading/punch/list', params)
}

/**
 * 分页获取打卡记录列表
 */
export function getPunchListPage(params?: { page?: number; limit?: number }) {
  return http.get<ApiResponse<PageResult<PunchRecordRespVO>>>('/reading/punch/page', params)
}

/**
 * 获取打卡详情
 */
export function getPunchDetail(id: number) {
  return http.get<ApiResponse<PunchRecordRespVO>>(`/reading/punch/${id}`)
}

/**
 * 更新打卡记录
 */
export function updatePunch(id: number, data: PunchUpdateReqVO) {
  return http.put<ApiResponse<boolean>>(`/reading/punch/${id}`, data)
}

/**
 * 删除打卡记录
 */
export function deletePunch(id: number) {
  return http.delete<ApiResponse<boolean>>(`/reading/punch/${id}`)
}

/**
 * 获取打卡日历
 */
export function getPunchCalendar(params: { yearMonth?: string }) {
  return http.get<ApiResponse<PunchCalendarRespVO>>('/reading/punch/calendar', params)
}

/**
 * 获取今日打卡记录
 */
export function getTodayPunch() {
  return http.get<ApiResponse<PunchRespVO | null>>('/reading/punch/today')
}

/**
 * 获取公开打卡动态列表
 */
export function getPublicPunchRecords(params?: { limit?: number }) {
  return http.get<ApiResponse<PunchRecordRespVO[]>>('/reading/punch/public/list', params)
}

/**
 * 分页获取公开打卡动态列表
 */
export function getPublicPunchRecordsPage(params?: { page?: number; limit?: number }) {
  return http.get<ApiResponse<PageResult<PunchRecordRespVO>>>('/reading/punch/public/page', params)
}

/**
 * 检查今日是否可以打卡
 */
export function canPunchToday() {
  return http.get<ApiResponse<boolean>>('/reading/punch/can-punch-today')
}

/**
 * 获取打卡统计
 */
export function getPunchStats() {
  return http.get<ApiResponse<PunchStatsRespVO>>('/reading/punch/user-stats')
}

/**
 * 获取打卡排行榜
 */
export function getPunchRanking(params?: { type?: 'streak' | 'total' | 'time', limit?: number }) {
  return http.get<ApiResponse<PunchRankingRespVO[]>>('/reading/punch/ranking', params)
}

/**
 * 获取打卡成就列表
 */
export function getPunchAchievements() {
  return http.get<ApiResponse<PunchAchievementRespVO[]>>('/reading/punch/achievements')
}

/**
 * 获取打卡提醒设置
 */
export function getPunchReminderSettings() {
  return http.get<ApiResponse<PunchReminderSettings>>('/reading/punch/reminder-settings')
}

/**
 * 更新打卡提醒设置
 */
export function updatePunchReminderSettings(data: PunchReminderSettings) {
  return http.put<ApiResponse<boolean>>('/reading/punch/reminder-settings', data)
}

/**
 * 分享打卡记录
 */
export function sharePunch(punchId: number) {
  return http.post<ApiResponse<PunchShareInfo>>(`/reading/punch/${punchId}/share`)
}

/**
 * 获取打卡模板列表
 */
export function getPunchTemplates() {
  return http.get<ApiResponse<PunchTemplate[]>>('/reading/punch/templates')
}

/**
 * 导出打卡记录
 */
export function exportPunchRecords(params: PunchExportParams) {
  return http.get<ApiResponse<string>>('/reading/punch/export', params)
}

/**
 * 获取打卡趋势数据
 */
export function getPunchTrends(params?: { period?: 'week' | 'month' | 'year' }) {
  return http.get<ApiResponse<Array<{
    date: string
    punchCount: number
    readingTime: number
    bookCount: number
  }>>>('/reading/punch/trends', params)
}

/**
 * 获取打卡热力图数据
 */
export function getPunchHeatmap(year: number) {
  return http.get<ApiResponse<Array<{
    date: string
    value: number
    level: number
  }>>>('/reading/punch/heatmap', { year })
}

/**
 * 批量删除打卡记录
 */
export function batchDeletePunch(ids: number[]) {
  return http.delete<ApiResponse<boolean>>('/reading/punch/batch-delete', { ids })
}

/**
 * 获取打卡建议
 */
export function getPunchSuggestions() {
  return http.get<ApiResponse<{
    recommendedReadingTime: number
    recommendedBooks: Array<{
      bookId: number
      bookTitle: string
      reason: string
    }>
    motivationalQuotes: string[]
    tips: string[]
  }>>('/reading/punch/suggestions')
}

/**
 * 获取打卡报告
 */
export function getPunchReport(params?: { startDate?: string, endDate?: string }) {
  return http.get<ApiResponse<{
    summary: {
      totalDays: number
      punchDays: number
      punchRate: number
      totalReadingTime: number
      totalBooks: number
      averageReadingTime: number
    }
    trends: Array<{
      date: string
      readingTime: number
      bookCount: number
    }>
    achievements: PunchAchievementRespVO[]
    recommendations: string[]
  }>>('/reading/punch/report', params)
}
